package cn.itsource.pethome.org.service.impl;

import cn.itsource.pethome.base.domain.BaseQuery;
import cn.itsource.pethome.base.domain.PageResult;
import cn.itsource.pethome.base.util.TreeUtil;
import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.mapper.DepartmentMapper;
import cn.itsource.pethome.org.mapper.EmployeeMapper;
import cn.itsource.pethome.org.service.DepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    EmployeeMapper employeeMapper;
    @Override
    public Department findOne(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void insert(Department department) {
        //1.新增部门
        //首先取出我们的员工id,然后赋值
        department.setManagerId(department.getManager().getId());
        departmentMapper.insertSelective(department);

    }

    /**
     * 高级分页查询
     * @param query
     * @return
     */
    //@Transactional   就处理事务的 ,可以保证事务acid
    @Override
    @Transactional
    public PageResult<Department> findPage(BaseQuery query) {
        //第一步:创建分页对象
        PageHelper.startPage(query.getPage(),query.getPageSize());
//        第二步:查询所有
        List<Department> departments = departmentMapper.findPage(query);
        //使用java8 stream流
        departments.stream().forEach(dept -> {
            //1.查出部门经理
            Employee employee = employeeMapper.selectByPrimaryKey(dept.getManagerId());
            dept.setManager(employee);
            //2.查出上级部门
            Department department = departmentMapper.selectByPrimaryKey(dept.getParentId());
            dept.setParent(department);
        });

        //第三步 :封装数据
        PageInfo<Department> departmentPageInfo = new PageInfo<>(departments);
        List<Department> list = departmentPageInfo.getList();
        long total = departmentPageInfo.getTotal();
        PageResult<Department> result = new PageResult<Department>(total, list);
        return result;
    }

    @Override
    public void update(Department department) {
        departmentMapper.updateByPrimaryKeySelective(department);
    }

    /**
     * 做的是逻辑删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        Department department = new Department();
        department.setId(id);
        department.setDel(Boolean.FALSE);
        departmentMapper.updateByPrimaryKeySelective(department);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        departmentMapper.deleteBatch(ids);
    }

    /**
     * 查询部门树列表
     * @return
     */
    @Override
    public List<Department> findTree() {
        //第一步:查询所有数据
        List<Department> departments = departmentMapper.selectAll();
        //第二步:调用treeUtil
        List<Department> tree = TreeUtil.getTree(departments);
        return tree;
    }
}
